{% extends "zato/index.html" %}

{% block html_title %}Guaranteed delivery{% endblock %}

{% block "extra_css" %}
    <link rel="stylesheet" type="text/css" href="/static/css/jquery.alerts.css">
    <link rel="stylesheet" type="text/css" href="/static/css/slideout.css">
{% endblock %}

{% block "extra_js" %}

    {% comment %} jQuery {% endcomment %}
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.tablesorter.min.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.cookie.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.alerts.min.js"></script>

    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/common.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/pattern/delivery/look-up-task.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/pattern/delivery/definition/index.js"></script>


    <script nonce="{{ CSP_NONCE }}">

    $.fn.zato.data_table.get_columns = function() {
        return [
            '_numbering',
            '_selection',
            'name',
            'target',
            'short_def',
            'total_count',
            'in_progress_count',
            'in_doubt_count',
            'arch_success_count',
            'arch_failed_count',
            'last_updated',
            'last_used',
            '_edit',
            '_delete',
            'id',
            'check_after',
            'retry_repeats',
            'retry_seconds',
            'expire_after',
            'expire_arch_succ_after',
            'expire_arch_fail_after',
            'callback_list',
        ]
    }
    </script>

{% endblock %}

{% block "content" %}
<h2 class="zato">Guaranteed delivery</h2>

{% if not zato_clusters %}
    {% include "zato/no-clusters.html" %}
{% else %}
    {% include "zato/user-message.html" %}

    {% include "zato/search-form.html" with page_prompt="Show definitions"%}


    {% if cluster_id and target_type%}
        <div class='page_prompt'>
            <a href="javascript:$.fn.zato.pattern.delivery.create()">Create a new guaranteed delivery definition</a>
        </div>


        <div id="markup">
            <table id="data-table">
                <thead>
                    <tr class='ignore'>
                        <th>&nbsp;</th>
                        <th>&nbsp;</th>
                        <th style="width:20%"><a href="#">Name</a></th>
                        <th><a href="#">Target</a></th>
                        <th><a href="#">Short def.</a></th>
                        <th><a href="#">Total</a></th>
                        <th><a href="#">In-progress</a></th>
                        <th><a href="#">In-doubt</a></th>
                        <th><a href="#">Arch. success</a></th>
                        <th><a href="#">Arch. failed</a></th>
                        <th><a href="#">Counters updated</a></th>
                        <th><a href="#">Last used</a></th>
                        <th>&nbsp;</th>
                        <th>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                </thead>

                <tbody>
                {% if items %}
                {% for item in items %}
                    <tr class="{% cycle 'odd' 'even' %}" id='tr_{{ item.id }}'>
                        <td class='numbering'>&nbsp;</td>
                        <td class="impexp"><input type="checkbox" /></td>
                        <td>{{ item.name }}</td>
                        <td>{{ item.target }}</td>
                        <td>{{ item.short_def }}</td>
                        <td>{{ item.total_count }}</td>
                        <td>{{ item.in_progress_count }} <a href="{% url "pattern-delivery-index" item.name item.target_type item.target 'in-progress-any' cluster_id %}">(list)</a></td>
                        <td>{{ item.in_doubt_count }} <a href="{% url "pattern-delivery-index" item.name item.target_type item.target 'in-doubt' cluster_id %}">(list)</a></td>
                        <td>{{ item.confirmed_count }} <a href="{% url "pattern-delivery-index" item.name item.target_type item.target 'confirmed' cluster_id %}">(list)</a></td>
                        <td>{{ item.failed_count }} <a href="{% url "pattern-delivery-index" item.name item.target_type item.target 'failed' cluster_id %}">(list)</a></td>
                        <td>{% if item.last_updated %}{{ item.last_updated }}{% else %}<span class="form_hint">(Never)</span>{% endif %}</td>
                        <td>{% if item.last_used %}{{ item.last_used }}{% else %}<span class="form_hint">(Never)</span>{% endif %}</td>
                        <td><a href="javascript:$.fn.zato.pattern.delivery.edit('{{ item.id }}')">Edit</a></td>
                        <td><a href="javascript:$.fn.zato.pattern.delivery.delete_('{{ item.id }}')">Delete</a></td>
                        <td class='ignore item_id_{{ item.id }}'>{{ item.id }}</td>
                        <td class='ignore'>{{ item.check_after }}</td>
                        <td class='ignore'>{{ item.retry_repeats }}</td>
                        <td class='ignore'>{{ item.retry_seconds }}</td>
                        <td class='ignore'>{{ item.expire_after }}</td>
                        <td class='ignore'>{{ item.expire_arch_succ_after }}</td>
                        <td class='ignore'>{{ item.expire_arch_fail_after }}</td>
                        <td class='ignore'>{{ item.callback_list }}</td>
                    </tr>
                {% endfor %}
                {% else %}
                    <tr class='ignore'>
                        <td colspan='20'>No results</td>
                    </tr>
                {% endif %}

                </tbody>
            </table>
        </div>

        <div id="create-div" class='data-popup ignore'>
            <div class="bd">
                <form novalidate action="{% url "pattern-delivery-create" %}" method="post" id="create-form">
                    <table class="form-data">

                        <tr>
                            <td style="vertical-align:middle;width:40%">Name</td>
                            <td>{{ create_form.name }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">Target type</td>
                            <td>{{ target_type_human }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">Target</td>
                            <td>{{ create_form.target }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">Seconds before first check</td>
                            <td>{{ create_form.check_after }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">How many repeats</td>
                            <td>{{ create_form.retry_repeats }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">Seconds between each repeat</td>
                            <td>{{ create_form.retry_seconds }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">Expiration time <span class="form_hint">(seconds)</span></td>
                            <td>{{ create_form.expire_after }}</td>
                        </tr>

                        <tr>
                            <td colspan="2">
                                <hr/>
                                When to remove delivery from archive <span class="form_hint">(hours)</span>
                            </td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">On success</td>
                            <td>
                                {{ create_form.expire_arch_succ_after }}
                                <span class="form_hint">(default: 72 hours = 3 days)</span>
                            </td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">On failure</td>
                            <td>
                                {{ create_form.expire_arch_fail_after }}
                                <span class="form_hint">(default: 168 hours = 1 week)</span>
                            </td>
                        </tr>

                        <tr>
                            <td colspan="2">
                                <hr/>
                                Delivery callback services <span class="form_hint">(each on its own line)</span>
                            </td>
                        </tr>

                        <tr>
                            <td colspan="2">
                                {{ create_form.callback_list }}
                            </td>
                        </tr>

                        <tr>
                            <td colspan="2" style="text-align:right">
                                <input type="submit" value="OK" />
                                <input type="hidden" name="target_type" value="{{ target_type }}" />
                                <button type='button' onclick='javascript:$.fn.zato.data_table.close(this)'>Cancel</button>
                            </td>
                        </tr>
                    </table>
                    <input type="hidden" id="cluster_id" name="cluster_id" value="{{ cluster_id }}" />
                </form>
            </div>
        </div>

        <div id="edit-div" class='data-popup ignore'>
            <div class="bd">
                <form novalidate action="{% url "pattern-delivery-edit" %}" method="post" id="edit-form">
                    <table class="form-data">

                        <tr>
                            <td style="vertical-align:middle;width:40%">Name</td>
                            <td><span id="item_name"></span></td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">Target type</td>
                            <td>{{ target_type_human }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">Target</td>
                            <td>{{ edit_form.target }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">Seconds before first check</td>
                            <td>{{ edit_form.check_after }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">How many repeats</td>
                            <td>{{ edit_form.retry_repeats }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">Seconds between each repeat</td>
                            <td>{{ edit_form.retry_seconds }}</td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">Expiration time <span class="form_hint">(seconds)</span></td>
                            <td>{{ edit_form.expire_after }}</td>
                        </tr>

                        <tr>
                            <td colspan="2">
                                <hr/>
                                When to remove delivery from archive <span class="form_hint">(hours)</span>
                            </td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">On success</td>
                            <td>
                                {{ edit_form.expire_arch_succ_after }}
                                <span class="form_hint">(default: 72 hours = 3 days)</span>
                            </td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle;width:40%">On failure</td>
                            <td>
                                {{ edit_form.expire_arch_fail_after }}
                                <span class="form_hint">(default: 168 hours = 1 week)</span>
                            </td>
                        </tr>

                        <tr>
                            <td colspan="2">
                                <hr/>
                                Delivery callback services <span class="form_hint">(each on its own line)</span>
                            </td>
                        </tr>

                        <tr>
                            <td colspan="2">
                                {{ edit_form.callback_list }}
                            </td>
                        </tr>

                        <tr>
                            <td colspan="2" style="text-align:right">
                                <input type="submit" value="OK" />
                                <input type="hidden" name="edit-target_type" value="{{ target_type }}" />
                                <button type='button' onclick='javascript:$.fn.zato.data_table.close(this)'>Cancel</button>
                            </td>
                        </tr>
                    </table>
                    <input type="hidden" id="id_edit-cluster_id" name="cluster_id" value="{{ cluster_id }}" />
                    <input type="hidden" id="id_edit-name" name="edit-name" value="" />
                    <input type="hidden" id="id_edit-id" name="id" />
                </form>
            </div>
        </div>

	<input type="hidden" id="cluster_id" value="{{ cluster_id }}" />
	{% include "zato/pattern/delivery/look-up-task.html" %}


    {% else %}

        <p class="big-notice">
            Please select both cluster and target type
        </p>

    {% endif %} {% comment %}cluster_id and target_type{% endcomment %}


{% endif %}{% comment %}not zato_clusters{% endcomment %}

{% endblock %}
